﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataAccess;
using SPIWareMVVM.ViewModels;
using System.Collections.ObjectModel;

namespace SPIWareMVVM.Views
{
    /// <summary>
    /// Interaction logic for CustomerList.xaml
    /// </summary>
    public partial class WorkOrderList : UserControl
    {
        Window1 mainWindow { get; set; }
        ApplicationViewModel applicationViewModel { get; set; }
        bool _displayClosedWorkOrders { get; set; }
        public bool DisplayClosedWorkOrders
        { 
            get { return _displayClosedWorkOrders; }
            set { _displayClosedWorkOrders = value; DisplayLists(); }
        }

        public WorkOrderList()
        {
            InitializeComponent();
            DisplayClosedWorkOrders = false;
        }

        protected void WorkOrders_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mainWindow = (Window1)Window.GetWindow((ListBox)sender);
            if (mainWindow != null)
            {
                //this.lbWorkOrders.ItemsSource = ApplicationData.ocWorkOrders;
                //DisplayClosedWorkOrders = false;

                applicationViewModel = (ApplicationViewModel)((ObjectDataProvider)mainWindow.Resources["applicationVM"]).ObjectInstance;

                materialEdit.DataContext = null;

                if (e.RemovedItems.Count > 0)
                {
                    applicationViewModel.SelectedWorkOrder = null;
                }

                if (e.AddedItems.Count > 0)
                {
                    applicationViewModel.SelectedWorkOrder = (WorkOrder)e.AddedItems[0];

                    //if (applicationViewModel.SelectedCustomer == null ||
                    //    (applicationViewModel.SelectedCustomer != null && applicationViewModel.SelectedCustomer != applicationViewModel.SelectedWorkOrder.Customer))
                    //    applicationViewModel.SelectedCustomer = applicationViewModel.SelectedWorkOrder.Customer;

                    dpWorkOrderEdit.Visibility = System.Windows.Visibility.Visible;
                    materialEdit.DataContext = applicationViewModel.SelectedWorkOrder.Part;
                    materialEdit.expPress.Header = string.Format("Press Machine: Size: {0}",
                        applicationViewModel.SelectedWorkOrder != null && applicationViewModel.SelectedWorkOrder.Part != null
                        ? applicationViewModel.SelectedWorkOrder.Part.PressSize
                        : 0);
                    materialEdit.expMaterials.Header = string.Format("Materials ( {0} )",
                            applicationViewModel.SelectedWorkOrder != null && applicationViewModel.SelectedWorkOrder.Part != null && applicationViewModel.SelectedWorkOrder.Part.MaterialType != null
                        ? applicationViewModel.SelectedWorkOrder.Part.MaterialType.MaterialType1
                        : string.Empty);
                    materialEdit.expBoxes.Header = string.Format("Box Size: {0}",
                            applicationViewModel.SelectedWorkOrder != null && applicationViewModel.SelectedWorkOrder.Part != null 
                        ? applicationViewModel.SelectedWorkOrder.Part.BoxSize
                        : string.Empty);                        
                    materialEdit.expMolding.Header = string.Format("Mold # {0} ",
                            applicationViewModel.SelectedWorkOrder != null && applicationViewModel.SelectedWorkOrder.Part != null
                        ? applicationViewModel.SelectedWorkOrder.Part.MoldNumber
                        : string.Empty);

                    //materialEdit.IsEnabled = false;

                    this.cboParts.ItemsSource = null;
                    ObservableCollection<Part> partsList = ApplicationData.ocParts;
                    if (applicationViewModel.SelectedCustomer != null) partsList = partsList.GetByCustomer(applicationViewModel.SelectedCustomer);
                    this.cboParts.ItemsSource = partsList;

                    List<Type> types = new List<Type> { typeof(WorkOrder) };
                    if (ApplicationData.SelectedCustomer != null) types.Add(typeof(Customer));
                    ApplicationData.ClearAllExcept(types);
                    mainWindow.HideLists();
                    mainWindow.workOrdersList.dpWorkOrderEdit.Visibility = Visibility.Visible;
                }
            }
        }

        protected void Part_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mainWindow = (Window1)Window.GetWindow((ComboBox)sender);
            if (mainWindow != null)
            {
                applicationViewModel = (ApplicationViewModel)((ObjectDataProvider)mainWindow.Resources["applicationVM"]).ObjectInstance;
                if (e.AddedItems.Count > 0)
                {
                    if (e.AddedItems[0].GetType() == typeof(CustomerPart))
                    {
                        Part selectedPart = (Part)((CustomerPart)e.AddedItems[0]).Part;
                        applicationViewModel.SelectedWorkOrder.Part = selectedPart;
                    }
                    else if (e.AddedItems[0].GetType() == typeof(Part))
                    {
                        Part selectedPart = (Part)e.AddedItems[0];
                        if (applicationViewModel.SelectedWorkOrder != null)
                        {
                            WorkOrder wo = applicationViewModel.SelectedWorkOrder;

                            wo.Part = selectedPart;
                            wo.DrawingNumber = selectedPart.DrawingNumber;
                            wo.DrawingRevision = selectedPart.DrawingRevision;
                            wo.MoldNumber = selectedPart.MoldNumber;
                            wo.PalletsNeeded = selectedPart.PartsPerPallet != null && selectedPart.PartsPerPallet > 0
                                ? wo.oQuantity / (int)selectedPart.PartsPerPallet
                                : 0;
                            wo.PartsPerBox = selectedPart.PartsPerBox != null ? (int)selectedPart.PartsPerBox : 0;
                            wo.PartsPerShot = selectedPart.PartsPerShot != null ? (int)selectedPart.PartsPerShot : 0;
                            wo.PressSize = selectedPart.PressSize != null ? ((int)selectedPart.PressSize).ToString() : "0";
                            wo.BoxesNeeded = selectedPart.PartsPerBox != null ? wo.oQuantity / (int)selectedPart.PartsPerBox : 0;
                            wo.ShotsNeeded = selectedPart.PartsPerShot != null ? wo.oQuantity / (int)selectedPart.PartsPerShot : wo.oQuantity;
                        }
                        if (applicationViewModel.SelectedCustomer != null)
                        {
                            bool found = false;
                            foreach (CustomerPart cp in applicationViewModel.SelectedCustomer.CustomerParts)
                            {
                                if (cp.PartsId == selectedPart.PartsId)
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                CustomerPart newCP = new CustomerPart();
                                newCP.PartsId = selectedPart.PartsId;
                                newCP.CustomerId = applicationViewModel.SelectedCustomer.CustomerId;
                                applicationViewModel.SelectedCustomer.CustomerParts.Add(newCP);
                            }
                        }

                        this.materialEdit.DataContext = selectedPart;
                        //this.materialEdit.Visibility = Visibility.Collapsed;
                    }
                }
            }
        }
        private WorkOrder NewWorkOrder()
        {
            WorkOrder newWO = new WorkOrder();
            newWO.DateReceived = DateTime.Now;
            newWO.CorrectedQuantity = 0;
            newWO.QuantityOrdered = 0;
            return newWO;
        }

        private void cboCustomers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Customer customer = (Customer)e.AddedItems[0];
                if (customer != null && applicationViewModel != null)
                {
                    if (applicationViewModel.SelectedWorkOrder != null) applicationViewModel.SelectedWorkOrder.Customer = customer;
                    applicationViewModel.SelectedCustomer = customer;
                }
            }
        }

        private void btnOpenClosed_Clicked(object sender, RoutedEventArgs e)
        {
            DisplayClosedWorkOrders = !DisplayClosedWorkOrders;
            //if (DisplayClosedWorkOrders)
            //{
            //    ObservableCollection<WorkOrder> list = ApplicationData.ocWorkOrders;
            //    if (ApplicationData.SelectedCustomer != null) list = list.GetByCustomer(ApplicationData.SelectedCustomer);
            //    this.lbWorkOrders.ItemsSource = list;

            //    DisplayClosedWorkOrders = false;
            //    this.btnOpenClosed.Content = string.Format("( {0} ) Displaying All", list.Count);
            //}
            //else
            //{
            //    ObservableCollection<WorkOrder> list = ApplicationData.ocWorkOrders.DisplayOnlyOpen();
            //    if (ApplicationData.SelectedCustomer != null) list = list.GetByCustomer(ApplicationData.SelectedCustomer);
            //    this.lbWorkOrders.ItemsSource = list;

            //    this.btnOpenClosed.Content = string.Format("( {0} ) Displaying Open", list.Count);
            //    DisplayClosedWorkOrders = true;
            //}
        }

        protected void DisplayLists()
        {
            ObservableCollection<WorkOrder> list = ApplicationData.ocWorkOrders;
            if (ApplicationData.SelectedCustomer != null) list = list.GetByCustomer(ApplicationData.SelectedCustomer);
            if (!DisplayClosedWorkOrders) list = list.DisplayOnlyOpen();

            this.lbWorkOrders.ItemsSource = list.OrderBy(o => o.WorkOrderId); ;
            this.btnOpenClosed.Content = string.Format("( {0} ) Displaying {1}"
                , list.Count
                , DisplayClosedWorkOrders ? "All" : "Open");

            if (cboParts.Items.Count <= 0) cboParts.ItemsSource = ApplicationData.ocParts;
            if (cboCustomers.Items.Count <= 0) cboCustomers.ItemsSource = ApplicationData.ocCustomers;
        }
        public void cmdPrintWO(object sender, EventArgs e)
        {
            if (mainWindow.workOrderReport.Visibility == Visibility.Visible)
            {
                PrintDialog pd = new PrintDialog();
                pd.PrintVisual(mainWindow.workOrderReport.dpReport, string.Empty);
            }
        }
    }
}
